package zcw.com.lib_leet_code;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 朱城委 on 2021/7/16.<br><br>
 *
 * 电话号码的字母组合
 */
public class Topic17 {

    public static void main(String[] args) {
        Topic17 instance = new Topic17();

        System.out.println(instance.letterCombinations(""));
        System.out.println(instance.letterCombinations("2"));
        System.out.println(instance.letterCombinations("23"));
    }

    public List<String> letterCombinations(String digits) {
        List<String> result = new ArrayList<>();
        if(digits.length() == 0) {
            return result;
        }

        String[] map = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};

        letterCombinations(digits, 0, new StringBuilder(), result, map);
        return result;
    }

    private void letterCombinations(String digits, int index, StringBuilder builder, List<String> result, String[] map) {
        if(index == digits.length()) {
            result.add(builder.toString());
            return ;
        }

        String str = map[digits.charAt(index) - '2'];
        for(char ch : str.toCharArray()) {
            builder.append(ch);
            letterCombinations(digits, index + 1, builder, result, map);
            builder.setLength(builder.length() - 1);
        }
    }
}
